﻿/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.4
import QtGraphicalEffects 1.0
import "qrc:/MaterialUI/"
import "qrc:/MaterialUI/Interface/"

Page {
    id: root
    signal itemClicked(var name,var mail,var online,var message,var index);

    Component.onCompleted: {

    }

    ListView {
        id: listView
        anchors.fill: parent
        topMargin: 5
        rightMargin: 5
        spacing: 0
        clip: true
        model: SqlContactModel
        currentIndex:SqlContactModel.currentIndex
        delegate: ItemDelegate {
            Rectangle{
                anchors.fill: parent
                color: listView.currentIndex!==index?"#00000000":"lightgray"
            }
            Rectangle{
                id:dropBackground
                anchors.fill: parent
                color: "lightskyblue"
                visible: false
            }

            MaterialLabel{
                id:listname
                text: model.user_name
                color: {
                    if(model.user_online==="0"){
                        listView.currentIndex===index?inconversationwith.text = model.user_name+"(离线)":""
                        return "gray";
                    }else{
                        listView.currentIndex===index?inconversationwith.text = model.user_name:""
                        return "black";
                    }
                }
                font.pixelSize: 20
                leftPadding: avatar.width + 28
                anchors.top: parent.top
                visible: model.user_mail==="GROUP"?false:true
            }
            MaterialLabel{
                id:listtime
                text: model.user_online_time.substr(11,5)
                color: "gray"
                font.pixelSize: 13
                anchors.top: parent.top
                anchors.topMargin: 6
                anchors.right: parent.right
                anchors.rightMargin: 15
            }

            MaterialLabel{
                text: model.user_mail
                font.pixelSize: 13
                color: "gray"
                anchors.top: listname.bottom
                leftPadding: avatar.width + 28
                visible: model.user_mail==="GROUP"?false:true
            }
            MaterialLabel{
                text: model.user_mail+" ("+listView.count+")"
                font.pixelSize: 20
                leftPadding: avatar.width + 28
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                verticalAlignment: Label.AlignVCenter
                visible: model.user_mail==="GROUP"?true:false
            }

            width: listView.width - listView.leftMargin - listView.rightMargin
            height: avatar.height+10
            leftPadding: avatar.width + 32
            onClicked: {
                if(listView.currentIndex==index)
                    return

                var temp_user_mail = model.user_mail
                var temp_user_online = model.user_online
                var temp_user_name = model.user_name

//                SqlContactModel.currentIndex=index
//                if(model.user_messages!=="0")
//                    SqlContactModel.clearMessageSum(index)

                if(temp_user_mail==="GROUP"){
                    itemClicked(temp_user_mail,temp_user_mail,temp_user_online,model.user_messages,index)
                }
                else{
                    itemClicked(temp_user_name,temp_user_mail,temp_user_online,model.user_messages,index)
                }
            }

            Image {
                id: avatar
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 5
                width: 40
                height: width
                source: model.user_mail==="GROUP"?"qrc:/group.png":"qrc:/"+model.user_img+".png"
                fillMode:Image.PreserveAspectFit
                layer.enabled: true
                layer.effect:DropShadow {
                    horizontalOffset: 0
                    verticalOffset: 0
                    radius: 4
                    samples: 9
                    color: "#000000"
                    spread:0
                }
                Rectangle{
                    anchors.fill: parent
                    visible: model.user_online==="0"?true:false
                    opacity:0.5
                }
            }
            Rectangle{
                id:messagesSum
                width: 14
                height: width
                radius: 7
                color: "#8BC34A"
                visible: model.user_messages!=="0"?true:false
                anchors{
                    left: avatar.right
                    top: avatar.top
                    leftMargin: -7
                    topMargin: -5
                }
                MaterialLabel{
                    text: model.user_messages
                    font.pixelSize: 10
                    anchors.centerIn: parent
                    onTextChanged: {
                        animation.running = true
                    }
                }
                PropertyAnimation {
                    id: animation;
                    target: messagesSum;
                    property: "scale";
                    to: 1.2;
                    duration: 200
                    onFinished: {
                        animation1.running = true
                    }
                }
                PropertyAnimation {
                    id: animation1;
                    target: messagesSum;
                    property: "scale";
                    to: 1;
                    duration: 100
                }
            }


            DropArea {
                anchors.fill: parent
                onEntered:{
                    dropBackground.visible = true
                }
                onExited:{
                    dropBackground.visible = false
                }
                onDropped: {
                    if( !drop.hasUrls ) { return; }
                    var filePaths = [ ];
                    for( var index = 0; index < drop.urls.length; ++index )
                    {
                        var url = drop.urls[ index ].toString();
                        console.log(url)
                        if ( url.indexOf( "file://" ) !== 0 ) { return; }
                        if ( Qt.platform.os === "windows" )
                            filePaths.push( url.substr( 8 ) );
                        else
                            filePaths.push( url.substr( 7 ) );
                    }
                    var rv = SqlConversationAll.sendFile(model.user_mail,filePaths);
                    if(rv.length!==0){
                        materialUI.showSnackbarMessage(rv)
                    }
                    dropBackground.visible = false
                }
            }
        }
    }


    Rectangle{
        anchors{
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            margins: 5
        }
        height: 30
        visible: ChatManage.MyInfo["user_mail"].indexOf("kong_w")!==-1?true:false

        TextEdit {
            id:mypi
            selectByMouse: true
            selectionColor: "#2799f3"
            text: ChatManage.MyPi
            font.pixelSize: 18
            anchors.centerIn: parent
            readOnly: true
            color: "#03A9F4"
            wrapMode: Label.Wrap
        }
        RectangularGlow {
            anchors.fill: parent
            glowRadius: 6
            spread: 0.22
            color: "#20000000"
        }
        MouseArea{
            anchors.fill: parent
            onPressed: {
//                mypi.forceActiveFocus()
//                mypi.selectAll()
                ChatManage.copyString(mypi.text)
            }
        }
    }
}
